// 2.0 默认递归，改变数组的length属性是无效的，对象不存在的属性是不能被劫持的


function reactive(target) {
  // console.log(target.__proto__.constructor);
  // if (target.proto instanceof Proxy) {
  //   // console.log('取消代理');
  //   return target
  // }
  // 创建响应式对象
  return createReactiveObject(target)
}

function createReactiveObject(target) {
  if (!isObject(target)) {
    return target
  }
  let baseHandler = {
    get(target, key, _receiver) {
      console.log('获取');
      let result = Reflect.get(target, key, _receiver)
      return isObject(result) ? reactive(result) : result //递归多层代理
      // return target[key]
    },
    set(target, key, value, _receiver) {
      let hadKey = target.hasOwnProperty(key)
      let oldValue = target[key]
      if (!hadKey) {
        console.log('新增');
      } else if (oldValue != value) {
        console.log('修改');
      }
      let res = Reflect.set(target, key, value, _receiver)
      return res
    }
  }
  return new Proxy(target, baseHandler)
}
function isObject(val) {
  return typeof val === 'object' && val !== null
}




let proxy = reactive({
  name: 'Lionel',
  age: 18
})

proxy.age = 20